Code:
+LIST P=16F506 ; DIRECTIVE TO DEFINE PROCESSOR ;-------------------------------------------------------------------------------------------------------------------------------------- ; FILE DESCRIPTION ;-------------------------------------------------------------------------------------------------------------------------------------- ; ; EVERY TIMER PERIOD THE MAIN CODE EXECUTES FIRST. THEN THE PROGRAM WILL WAIT FOR THE TMR0 VALUE TO REACH 255 SO THAT IT WILL KNOW ; WHEN THE TMR0 ROLLOVER IS ABOUT TO HAPPEN AND SYNCHRONIZES ON THAT ROLLOVER TO REACH INSTRUCTION CYCLE PRECISION. ; ; THE FOLLOWING CODE DEMONSTRATES HOW TIMED EVENTS CAN BE CREATED USING TIMER0, THE PERIOD IS FIXED AND IS THE PERIOD OF THE TIMER0 ; ROLLOVER (DIFFERENT PRESCALER VALUES MAY BE USED) ; TIMER0 MAY BE PRELOADED FROM FOR DIFFERENT TIMING (PLEASE SEE NOTE *ADVANCED * BELOW) ; ; THE MAIN LOOP WITH USER CODE DOES NOT HAVE TO HAVE ISOCHRONOUS LOOPS ! ANYTHING UP TO 65276 INSTRUCTION CYCLES WILL DO ! ; ;-------------------------------------------------------------------------------------------------------------------------------------- ; VARIABLE DEFINITION ;-------------------------------------------------------------------------------------------------------------------------------------- DELAYCOUNT EQU H'0D' ; USED IN A FIXED DELAY ;-------------------------------------------------------------------------------------------------------------------------------------- ; PROGRAM CODE ;-------------------------------------------------------------------------------------------------------------------------------------- ORG H'0000' ; RESET VECTOR INITIALIZE MOVLW B'11000111' ; CONFIGURE TIMER0 TO RUN FROM INSTRUCTION CLOCK WITH 1:256 PRESCALER OPTION ; WE WILL HAVE ROLLOVERS EVERY 65.5 MS (15.3 HZ) @ FOSC = 4 MHZ MOVLW D'254' ; PRELOAD TIMER0 FOR FAST STARTUP MOVWF TMR0 ; ;------- ----------------------------------------------------------------------------------------------- SYNCHRONIZE COMF TMR0,W ; ROUGH SYNCHRONISATION: WAIT FOR TIMER0 TO REACH 255 SKPZ ; IS TMR0 NOW 255 ? GOTO SYNCHRONIZE ; NO, GO LOOK AGAIN ; TIMER0 NOW HAS VALUE 255, THE PRESCALER VALUE IS NOW 3,4,5 OF 6 ;------- ----------------------------------------------------------------------------------------------- ; HERE THERE MUST BE A DELAY OF EXACTLY 248 INSTRUCTION CYCLES WHEN USING A 1:256 PRESCALER ; (CAN BE ANY USEFUL CODE) MOVLW D'82' ; 247 INSTRUCTION CYCLES MOVWF DELAYCOUNT ; DECFSZ DELAYCOUNT,F ; GOTO $-1 ; NOP ; 1 INSTRUCTION CYCLE ;------- ----------------------------------------------------------------------------------------------- ; FINE SYNCHRONISATION: WE WILL ARRIVE HERE JUST BEFORE THE PRESCALER ROLLS OVER (AND ; INCREMENTS TIMER0), HERE WE WILL COUNT HOW MANY INSTRUCTION CYCLES WE ARRIVED BEFORE THE ; ACTUAL ROLLOVER - WHICH GIVES US THE TIME WE NEED TO DELAY BCF STATUS,5 ; SELECT PAGE 0, WE CAN ONLY JUMP TO A DESTINATION IN THE LOWER 256 INSTRUCTIONS OF A MEMORY ; PAGE, A MEMORY PAGE IS 512 INSTRUCTIONS IN SIZE MOVLW D'3' ; ASSUME WE WILL HAVE TO SKIP THE DELAY (JUMP THREE INSTRUCTIONS) ADDWF TMR0,W ; W = UNCHANGED OR W = W - 1 (ADD TO THE DELAY DEPENDING ON HOW MUCH TO SOON WE ARRIVED) ADDWF TMR0,W ; W = UNCHANGED OR W = W - 1 (ADD TO THE DELAY DEPENDING ON HOW MUCH TO SOON WE ARRIVED) ADDWF TMR0,W ; W = UNCHANGED OR W = W - 1 (ADD TO THE DELAY DEPENDING ON HOW MUCH TO SOON WE ARRIVED) ; W IS NOW 0,1,2 OF 3 (0 = THREE CYCLES DELAY, 3 = NO DELAY) ADDWF PCL,F ; DELAY AS MUCH AS NEEDED /JUMP AHEAD AS MUCH AS NEEDED NOP ; PCL + 0 NOP ; PCL + 1 NOP ; PCL + 2 ;------- ----------------------------------------------------------------------------------------------- PSEUDO_INT ; PCL + 3 ; THE TIMER0 VALUE IS NOW 0, THE PRESCALER VALUE WILL ALWAYS BE 4 ; WE WILL GET BACK HERE EVERY 65536 INSTRUCTIONS (1:256 PRESCALER) ; WE MAY USE UP TO 65276 INSTRUCTION CYCLES HERE (1:256 PRESCALER) ;MOVLW D'32' ; * ADVANCED * ;MOVWF TMR0 ; NOTE:TO CHANGE THE PERIOD, WE CAN PRELOAD TIMER0 HERE, OR, WE CAN DELAY A BIT FIRST TO CHANGE ; THE PRESCALER VALUE, AND THEN PRELOAD TIMER0) ; * ADVANCED * NOP ; DO SOMETHING NOP ; DO SOMETHING MORE ; ETC... ; ETC... ; ETC... ;------- ----------------------------------------------------------------------------------------------- GOTO SYNCHRONIZE ; ;--------------------------------------------------------------------------------------------------------------------------------------
file: /Techref/member/AT-planet-T9/High precision timed events without interrupts for 12-bit PIC (instruction cycle accurate) using a non-isochronous main loop.htm, 6KB, , updated: 2010/10/2 12:57, local time: 2024/11/21 23:11,
owner: AT-planet-T9,
18.188.63.71:LOG IN
|
©2024 These pages are served without commercial sponsorship. (No popup ads, etc...).Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE. Questions? <A HREF="http://ecomorder.com/Techref/member/AT-planet-T9/High precision timed events without interrupts for 12-bit PIC (instruction cycle accurate) using a non-isochronous main loop.htm"> High precision timed events without interrupts for 12-bit PIC (instruction cycle accurate) using a non-isochronous main loop</A> |
Did you find what you needed? |